package com.pur.zsms.supplier.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.cglib.beans.BeanCopier;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.accountmanage.vo.AccountVo;
import com.pur.zsms.check.vo.CheckVo;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.ExcelReader;
import com.pur.zsms.common.util.FileUtill;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.documentsmanage.vo.DomVo;
import com.pur.zsms.lockmanage.service.ILockTransService;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.MaterVoForCostSupplier;
import com.pur.zsms.matermanage.mater.vo.MaterVoForSup;
import com.pur.zsms.matermanage.mater.vo.OrderDistributorMaterRealVo;
import com.pur.zsms.note.service.INoteService;
import com.pur.zsms.ordercustommanage.po.OrderDistributorPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.service.ISelectFoodService;
import com.pur.zsms.ordercustommanage.service.InsertCusFoodService;
import com.pur.zsms.ordercustommanage.vo.ExcelVo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.OrderDistributorVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterRealVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterVoFooter;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierVoFooter;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.pmsnmanage.menu.po.MenuPo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.pmsnmanage.user.web.UserControllor;
import com.pur.zsms.service.IDistributorService;
import com.pur.zsms.shopmanage.po.ShopPo;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.storemanage.po.StoreInPo;
import com.pur.zsms.storemanage.po.StoreOutPo;
import com.pur.zsms.storemanage.service.IStoreService;
import com.pur.zsms.storemanage.vo.StoreInVo;
import com.pur.zsms.storemanage.vo.StoreOutVo;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.supplier.service.ISupplierOrderService;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.supplier.vo.ShopSupplierVo;
import com.pur.zsms.supplier.vo.SupplierDistributeVo;
import com.pur.zsms.supplier.vo.SupplierOrderCustVo;
import com.pur.zsms.supplier.vo.SupplierQualificationVo;
import com.pur.zsms.supplier.vo.SupplierVo;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.systemsetting.datadictionary.vo.CodeVo;
import com.pur.zsms.timecontrol.service.ITimeControlService;

/**
 * 供应商控制器
 * 
 * @author yuxin
 * 
 */
@Controller
@RequestMapping(value = "/distributorForSup")
public class DistributorControllerForSup {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(DistributorControllerForSup.class);
	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@Resource(name = "distributorService")
	private IDistributorService distributorService;

	@Resource(name = "timeControlService")
	private ITimeControlService timeControlService;

	@Resource(name = "userService")
	private IUserService userService;

	@Resource(name = "lockTransService")
	private ILockTransService lockTransService;

	@Resource(name = "storeService")
	private IStoreService storeService;

	@Resource(name = "shopService")
	private IShopService shopService;

	@Resource(name = "noteService")
	private INoteService noteService;

	@Resource(name = "selectFoodService")
	private ISelectFoodService selectFoodService;

	@Resource(name = "insertCusFoodService")
	private InsertCusFoodService insertCusFoodService;

	@Resource(name = "supplierOrderService")
	private ISupplierOrderService supplierOrderService;

	@RequestMapping(value = "/findMaterForDistributor.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findMaterForDistributor(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, MaterVo materVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		UserPo userInfo = (UserPo) session.getAttribute("user");
		materVo.setSupId(supplierService.getSupIdByUserId(userInfo.getUserId()));
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = distributorService.findMaterForDistributor(pageController, materVo);
		List<MaterVo> materList = new ArrayList<MaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				MaterVo f = new MaterVo();
				f.setMaterId((Integer) item[0]);
				f.setMaterName((String) item[1]);
				f.setMaterType((Integer) item[2]);
				f.setUnit((String) item[3]);
				f.setSpecifications((String) item[4]);
				f.setBrands((String) item[5]);
				f.setMaterStoreNumber((BigDecimal) item[6]);
				f.setMaterLevel((Integer) item[7]);
				materList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", materList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 选择材料
	 * */
	@RequestMapping(value = "/commitMaterOrderForDistributor.do", method = { RequestMethod.POST })
	@ResponseBody
	public String commitMaterOrderForDistributor(OrderDistributorMaterRealVo orderDistributorMaterRealVo, HttpSession session) {

		UserPo userInfo = (UserPo) session.getAttribute("user");
		JSONObject json1 = new JSONObject();
		String nowTime = PubFun.getCurrentDate() + " " + PubFun.getCurrentTime();

		try {
			long ordDistributorId = supplierService.getSupIdByUserId(userInfo.getUserId());
			orderDistributorMaterRealVo.setOrdDistributorId(ordDistributorId);

			// 增加校验 每天只能提交一次采购清单-----------下
			int ordId = 0;
			/*
			 * int ordId =
			 * selectFoodService.checkCommitMaterOrderForSup(selectMaterVo
			 * .getOrderDate(), shopId, supId); if (ordId != 0) {
			 * json1.put("success", false); json1.put("msg",
			 * selectFoodService.getMsg()); return json1.toString(); }
			 */
			// 增加校验 每天只能提交一次采购清单-----------上

			// 根据日期取得星期几-----------下
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			date = format.parse(orderDistributorMaterRealVo.getOrderDate());
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
			if (dayOfWeek < 0)
				dayOfWeek = 0;
			orderDistributorMaterRealVo.setOrderDay(dayNames[dayOfWeek]);
			// 根据日期取得星期几-----------上
			JSONArray array = JSONArray.fromObject(orderDistributorMaterRealVo.getMaterdategridList());

			List<OrderDistributorMaterRealVo> orderDistributorMaterRealVoList = new ArrayList<OrderDistributorMaterRealVo>();
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				OrderDistributorMaterRealVo tOrderDistributorMaterRealVo = new OrderDistributorMaterRealVo();
				tOrderDistributorMaterRealVo.setOrdMaterId(Integer.valueOf(object.get("ordMaterId").toString()));
				tOrderDistributorMaterRealVo.setOrdMaterWeight(new BigDecimal(object.get("materWeight").toString()));
				tOrderDistributorMaterRealVo.setOrdMaterLevel(Integer.valueOf(object.get("materLevel").toString()));
				tOrderDistributorMaterRealVo.setCode(object.get("code").toString());
				tOrderDistributorMaterRealVo.setOrdSupId(orderDistributorMaterRealVo.getOrdSupId());
				tOrderDistributorMaterRealVo.setOrdDistributorId(ordDistributorId);
				tOrderDistributorMaterRealVo.setOrderDate(orderDistributorMaterRealVo.getOrderDate());
				tOrderDistributorMaterRealVo.setOrderDay(orderDistributorMaterRealVo.getOrderDay());
				orderDistributorMaterRealVoList.add(tOrderDistributorMaterRealVo);
			}
			// 先判断 存在不存在此供货商的ordID

			// 往客户订单表中插入值
			// ordId =
			// selectFoodService.checkCommitMaterOrder(selectMaterVo.getOrderDate(),
			// shopId, 0);
			OrderDistributorPo orderDistributorPo = distributorService.addMaterOrderForDistributor(orderDistributorMaterRealVoList);

			distributorService.addMaterOrderDetailForDistributor(orderDistributorMaterRealVoList, orderDistributorPo.getOrdId());

			/*
			 * noteService.sendMessageToSup(String.valueOf(ordId),
			 * orderDistributorMaterRealVo.getOrderDate(),
			 * shopService.getShopPo(shopId) .getShopName());
			 */
			json1.put("success", true);
		} catch (Exception e) {
			log.info(e.toString());
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/findDistributorOrderForSup.do")
	@ResponseBody
	public String findDistributorOrderForSup(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			OrderDistributorMaterRealVo orderDistributorMaterRealVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");
		// 根据用户id得出supid
		int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
		orderDistributorMaterRealVo.setOrdSupId(supId);

		this.pageList = distributorService.listDistributorOrder(pageController, orderDistributorMaterRealVo);

		List<OrderDistributorMaterRealVo> orderDistributorMaterRealVoList = new ArrayList<OrderDistributorMaterRealVo>();
		List<OrderDistributorMaterRealVo> orderDistributorMaterRealVoListFooter = new ArrayList<OrderDistributorMaterRealVo>();
		BigDecimal ordValueReal = new BigDecimal(0);
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderDistributorMaterRealVo f = new OrderDistributorMaterRealVo();

				f.setOrdId((Integer) item[0]);
				f.setOrdSupId((Integer) item[1]);
				f.setOrdSupName((String) item[2]);
				f.setOrdStateDistributor((Integer) item[3]);
				f.setOrderDate((String) item[4]);
				f.setOrderDay((String) item[5]);
				if (item[6] != null) {
					f.setOrdValue(new BigDecimal(item[6].toString()));
					ordValueReal.add(new BigDecimal(item[6].toString()));
				} else {
					f.setOrdValue(new BigDecimal(0));
				}
				f.setOrdDistributorId(Long.valueOf(item[8].toString()));
				f.setOrdDisName((String) item[9]);

				orderDistributorMaterRealVoList.add(f);
			}
		}

		OrderDistributorMaterRealVo footer = new OrderDistributorMaterRealVo();
		footer.setOrdSupName("合计");
		footer.setOrdValue(ordValueReal);
		orderDistributorMaterRealVoListFooter.add(footer);
		json.put("footer", orderDistributorMaterRealVoListFooter);

		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderDistributorMaterRealVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 进入供货商详细订单
	 * */
	@RequestMapping(value = "/goDistributorDetailedForSup.do", method = { RequestMethod.GET })
	public ModelAndView goDistributorDetailedForSup(int id, int ordDistributorId, boolean flag, HttpSession session) {
		/*
		 * FoodVo foodVo = new FoodVo(); try {
		 * 
		 * foodVo = foodService.getFoodid(foodId); // 查询所有角色信息 } catch
		 * (GenericException e) { e.printStackTrace(); }
		 */
		Map map = new HashMap();
		/*
		 * map.put("ordId", id); map.put("supId", supId);
		 */

		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			// 取得订单状态
			OrderDistributorVo tOrderDistributorVo = new OrderDistributorVo();
			tOrderDistributorVo.setOrdSupId((int) supId);
			tOrderDistributorVo.setOrdDistributorId(ordDistributorId);
			tOrderDistributorVo.setOrdId(id);

			OrderDistributorPo tOrderDistributorPo = distributorService.getOrderDistributorPo(tOrderDistributorVo);
			map.put("OrderDistributorPo", tOrderDistributorPo);

			if (flag) {
				return new ModelAndView("/distributor/distributorDetailedIndexForSup", map);
			} else {
				return new ModelAndView("/jsp-mobile/ordermanage/distributorDetailedIndexForSup_mobile", map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("/base/error", map);
		}
	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/findDistributorDetailOrder.do")
	@ResponseBody
	public String findDistributorDetailOrder(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			OrderDistributorMaterRealVo orderDistributorMaterRealVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";

		// 设置rows 长度
		rows = "999";
		UserPo userInfo = (UserPo) session.getAttribute("user");
		// 根据用户id得出supid
		int ordDistributorId = supplierService.getSupIdByUserId(userInfo.getUserId());
		orderDistributorMaterRealVo.setOrdDistributorId(ordDistributorId);

		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = distributorService.findDistributorDetailOrder(pageController, orderDistributorMaterRealVo);
		List<OrderDistributorMaterRealVo> orderDistributorMaterRealVoList = new ArrayList<OrderDistributorMaterRealVo>();
		List items = pageList.getItems();

		BigDecimal ordValueRealAll = new BigDecimal(0);

		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderDistributorMaterRealVo f = new OrderDistributorMaterRealVo();
				f.setOrdId(Integer.valueOf(item[0].toString()));
				f.setOrdDistributorMaterId(Integer.valueOf(item[1].toString()));
				f.setCode(item[2].toString());
				f.setOrdMaterId(Integer.valueOf(item[3].toString()));
				f.setMaterName(item[4].toString());
				f.setMaterType(Integer.valueOf(item[5].toString()));
				f.setBrands(item[6].toString());
				f.setSpecifications(item[7].toString());
				f.setUnit(item[8].toString());
				f.setOrdMaterLevel(Integer.valueOf(item[9].toString()));
				if (item[10] != null) {
					f.setOrdMaterValueUnit(new BigDecimal(item[10].toString()));
				} else {
					f.setOrdMaterValueUnit(new BigDecimal(0));
				}
				if (item[11] != null) {
					f.setOrdMaterValue(new BigDecimal(item[11].toString()));
				} else {
					f.setOrdMaterValue(new BigDecimal(0));
				}
				if (item[12] != null) {
					f.setOrdMaterWeight(new BigDecimal(item[12].toString()));
				} else {
					f.setOrdMaterWeight(new BigDecimal(0));
				}
				if (item[13] != null) {
					f.setOrdMaterWeightReality(new BigDecimal(item[13].toString()));
				} else {
					f.setOrdMaterWeightReality(new BigDecimal(0));
				}
				f.setOrdSupId(Integer.valueOf(item[14].toString()));
				f.setOrdSupName(item[15].toString());
				orderDistributorMaterRealVoList.add(f);
			}
		}

		List<OrderSupplierMaterVoFooter> orderSupplierVoListFooter = new ArrayList<OrderSupplierMaterVoFooter>();
		OrderSupplierMaterVoFooter footer = new OrderSupplierMaterVoFooter();
		footer.setOrdCustName("合计");
		footer.setOrdMaterValueReal(ordValueRealAll);
		orderSupplierVoListFooter.add(footer);
		json.put("footer", orderSupplierVoListFooter);

		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderDistributorMaterRealVoList);
		return json.toString();
	}

	// 导入价格和数量
	@RequestMapping(value = "/updateMaterWeightAndMaterValueForDistributorAndSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateMaterWeightAndMaterValueForDistributorAndSup(OrderDistributorMaterRealVo orderDistributorMaterRealVo,
			HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		JSONObject json1 = new JSONObject();
		UserPo lgUser = (UserPo) session.getAttribute("user");
		int supId = supplierService.getSupIdByUserId(lgUser.getUserId());

		// 事物锁
		boolean lockFlag = lockTransService.lockTrans("updateMaterWeightAndMaterValue.do", supId);
		if (!lockFlag) {
			json1.put("msg", "locked");
			json1.put("success", false);
			return json1.toString();
		}

		orderDistributorMaterRealVo.setOrdSupId(supId);
		boolean flag = false;
		try {
			OrderDistributorVo orderDistributorVo = new OrderDistributorVo();
			orderDistributorVo.setOrdDistributorId(orderDistributorMaterRealVo.getOrdDistributorId());
			orderDistributorVo.setOrdId(orderDistributorMaterRealVo.getOrdId());
			orderDistributorVo.setOrdSupId(orderDistributorMaterRealVo.getOrdSupId());
			// 校验状态
			OrderDistributorPo orderDistributorPo = distributorService.getOrderDistributorPo(orderDistributorVo);

			if (orderDistributorPo.getOrdStateDistributor() == 1 || orderDistributorPo.getOrdStateDistributor() == 2) {
				distributorService.updateOrderForDistributor(orderDistributorMaterRealVo);
				distributorService.updateOrderMaterValueAll(orderDistributorPo, true);
				json1.put("success", true);
			} else {
				json1.put("msg", "订单已被确认或者状态不正确！");
				json1.put("success", false);
			}
			// supplierService.computeTotalValue(materVo.getDate(),
			// materVo.getSupId());
		} catch (Exception e) {
			json1.put("msg", supplierService.getMsg());
			flag = false;
		} finally {
			// 解锁
			lockTransService.unlockTrans("updateMaterWeightAndMaterValue.do", supId);
		}
		return json1.toString();
	}

	@RequestMapping(value = "/deleteDistributorOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteDistributorOrder(String jsonString, HttpSession session, OrderDistributorMaterRealVo orderDistributorMaterRealVo) {
		JSONObject json1 = new JSONObject();
		try {
			UserPo lgUser = (UserPo) session.getAttribute("user");
			int distributorId = supplierService.getSupIdByUserId(lgUser.getUserId());

			orderDistributorMaterRealVo.setOrdDistributorId(distributorId);

			OrderDistributorVo orderDistributorVo = new OrderDistributorVo();
			orderDistributorVo.setOrdDistributorId(distributorId);
			orderDistributorVo.setOrdId(orderDistributorMaterRealVo.getOrdId());
			orderDistributorVo.setOrdSupId(orderDistributorMaterRealVo.getOrdSupId());
			// 校验状态
			OrderDistributorPo orderDistributorPo = distributorService.getOrderDistributorPo(orderDistributorVo);

			if (orderDistributorPo.getOrdStateDistributor() == 1 || orderDistributorPo.getOrdStateDistributor() == 2) {
				distributorService.deleteOrderMaterForDistributor(orderDistributorMaterRealVo);

				json1.put("success", true);
			} else {
				json1.put("msg", "订单已被确认或者状态不正确！");
				json1.put("success", false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}

	@RequestMapping(value = "/reviewOrderDistributor.do", method = { RequestMethod.POST })
	@ResponseBody
	public String reviewOrderDistributor(String jsonString, HttpSession session, OrderDistributorMaterRealVo orderDistributorMaterRealVo) {
		JSONObject json1 = new JSONObject();
		try {
			UserPo lgUser = (UserPo) session.getAttribute("user");
			int distributorId = supplierService.getSupIdByUserId(lgUser.getUserId());

			orderDistributorMaterRealVo.setOrdDistributorId(distributorId);

			OrderDistributorVo orderDistributorVo = new OrderDistributorVo();
			orderDistributorVo.setOrdDistributorId(distributorId);
			orderDistributorVo.setOrdId(orderDistributorMaterRealVo.getOrdId());
			orderDistributorVo.setOrdSupId(orderDistributorMaterRealVo.getOrdSupId());
			// 校验状态
			OrderDistributorPo orderDistributorPo = distributorService.getOrderDistributorPo(orderDistributorVo);

			if (orderDistributorPo.getOrdStateDistributor() == 1 || orderDistributorPo.getOrdStateDistributor() == 2) {
				distributorService.reviewOrderDistributor(orderDistributorPo);
				// 产生入库单

				UserPo userInfo = (UserPo) session.getAttribute("user");
				int storeHouseId = storeService.getStoreHouseIdBySupId(distributorId);

				StoreInVo storeInVo = new StoreInVo();
				storeInVo.setStoreHouseId(storeHouseId);
				storeInVo.setStoreInState(1);// 状态未确认
				storeInVo.setStoreInWay(2);// 订单自动入库
				storeInVo.setOrdId(orderDistributorPo.getOrdId());
				storeInVo.setSupId(distributorId);// 供货商
				storeInVo.setSupSupId(orderDistributorMaterRealVo.getOrdSupId());
				storeInVo.setCreatDate(orderDistributorMaterRealVo.getOrderDate());

				StoreInPo storeInPo = storeService.addStoreInForSup(storeInVo);
				storeInVo.setStoreInId(storeInPo.getStoreInId());
				// 自动入库
				storeService.addStoreInDetailedForDistributor(storeInVo, storeInPo);

				json1.put("success", true);
			} else {
				json1.put("msg", "订单已被审核或者状态不正确！");
				json1.put("success", false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			json1.put("success", false);
			json1.put("msg", e.getMessage());
		}
		return json1.toString();
	}
}
